home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_333 / multiplot / source / mplot_src / util.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  130 lines

  1. #include <ctype.h>
  2. #include <exec/types.h>
  3. #include "mp.h"
  4. extern int debug;
  5. /*************************************************************
  6. * Round a positive integer to the "simplest" number in [val..limit]
  7. */
  8.  
  9. #define lsdig(x)  (((x) >= 10) ? (x)-((x)/10)*10 : (x))
  10.  
  11. iround(val,dir,limit)
  12. int val, dir;
  13. float limit;
  14.  
  15. {
  16.    int tmpval;
  17.  
  18.    switch (dir) {
  19.       case UP:
  20.          tmpval = lsdig(val);
  21.          tmpval = ( (tmpval) ? (val+10-tmpval) : val );
  22.          if (tmpval > limit) return(val);
  23.          if (val <= 10) return(tmpval);
  24.          val = iround(tmpval/10,dir,limit/10);
  25.          return(val*10);
  26.       case DOWN:
  27.          tmpval = val-lsdig(val);
  28.          if (tmpval < limit) return(val);
  29.          if (val <= 10) return(tmpval);
  30.          val = iround(tmpval/10,dir,limit/10);
  31.          return(val*10);
  32.    }
  33.    return(val);
  34. }
  35.  
  36.  
  37. /*************************************************************
  38. * Round an FFP to the "simplest" number in [val..limit]
  39. */
  40.  
  41. #define FLARGE 1.0e18
  42.  
  43. FFP fround(ffpval, dir, ffplimit)
  44. FFP ffpval, ffplimit;
  45. int dir;
  46. {
  47.    FFP val, limit, fact=1.;
  48.  
  49.    val = ffpval;
  50.    limit = ffplimit;
  51.  
  52.    if (abs(val) < 1.e-4) return((FFP)0.);
  53.    if (val < 0.) {
  54.       val *= -1.; limit *= -1.; fact = -1.;
  55.       dir = (dir==UP ? DOWN : UP);
  56.    }
  57.  
  58.    while (val < 1.e5) {val*=10.; limit*=10.; fact*=10.;}
  59.    while (val > 1.e6) {val*=0.1; limit*=0.1; fact*=0.1;}
  60.    if ((int)val) {
  61.       val = (FFP) iround( (int)val, dir, limit);
  62.       val /= fact;
  63.       return(val);
  64.    }
  65.    else {
  66.       return(0.0);
  67.    }
  68. }
  69.  
  70.  
  71. /***************************************************************************/
  72. char *getwrd(bp)
  73. char **bp;
  74. {
  75.    char *retval;
  76.  
  77.    while (**bp == ' ') (*bp)++;
  78.    retval = *bp;
  79.    while (**bp && (**bp != ' ') ) (*bp)++;
  80.    return(retval);
  81. }
  82.  
  83.  
  84. /***************************************************************************/
  85. numeric(cp)
  86. char *cp;
  87. {
  88.    int rv = FALSE;
  89.    char c;
  90.  
  91.    while(*(cp) == ' ') cp++;
  92.  
  93.    while (c = *(cp++)) {
  94.       if (isdigit(c)||(toupper(c)=='E')||(c=='-')||(c=='+')||(c=='.'))
  95.          rv = TRUE;
  96.       else if ( c==' ')
  97.          return(rv);
  98.       else
  99.          return(FALSE);
  100.    }
  101.    return(rv);
  102. }
  103.  
  104.  
  105. /***************************************************************************/
  106. same(a,b,len)
  107. char *a, *b;
  108. int len;
  109. {
  110.    char i=0;
  111.  
  112.    while(*a && *b) {
  113.       if (toupper(*a) != toupper(*b)) return(FALSE);
  114.       a++; b++; i++;
  115.       if (i==len) return(TRUE);
  116.    }
  117.    return(i==len);
  118. }
  119.  
  120. /***************************************************************************/
  121. void trch(oldch,newch,buf)
  122. char oldch, newch, *buf;
  123. {
  124.    while (*buf) {
  125.       if (*buf == oldch) *buf = newch;
  126.       buf++;
  127.    }
  128. }
  129.  
  130.